<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="起始机构" prop="startAgencyId">
      <el-input v-model="dataForm.startAgencyId" placeholder="起始机构"></el-input>
    </el-form-item>
    <el-form-item label="终点机构" prop="endAgencyId">
      <el-input v-model="dataForm.endAgencyId" placeholder="终点机构"></el-input>
    </el-form-item>
    <el-form-item label="验证是否可用key,md5(子表的起始相加)" prop="verifyKey">
      <el-input v-model="dataForm.verifyKey" placeholder="验证是否可用key,md5(子表的起始相加)"></el-input>
    </el-form-item>
    <el-form-item label="距离" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="距离"></el-input>
    </el-form-item>
    <el-form-item label="成本" prop="cost">
      <el-input v-model="dataForm.cost" placeholder="成本"></el-input>
    </el-form-item>
    <el-form-item label="预计时间" prop="estimatedTime">
      <el-input v-model="dataForm.estimatedTime" placeholder="预计时间"></el-input>
    </el-form-item>
    <el-form-item label="中转次数" prop="transferCount">
      <el-input v-model="dataForm.transferCount" placeholder="中转次数"></el-input>
    </el-form-item>
    <el-form-item label="从1开始递增" prop="version">
      <el-input v-model="dataForm.version" placeholder="从1开始递增"></el-input>
    </el-form-item>
    <el-form-item label="是否是当前最新版本 0 否 1 是" prop="isCurrent">
      <el-input v-model="dataForm.isCurrent" placeholder="是否是当前最新版本 0 否 1 是"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          startAgencyId: '',
          endAgencyId: '',
          verifyKey: '',
          distance: '',
          cost: '',
          estimatedTime: '',
          transferCount: '',
          version: '',
          isCurrent: '',
          createDate: ''
        },
        dataRule: {
          startAgencyId: [
            { required: true, message: '起始机构不能为空', trigger: 'blur' }
          ],
          endAgencyId: [
            { required: true, message: '终点机构不能为空', trigger: 'blur' }
          ],
          verifyKey: [
            { required: true, message: '验证是否可用key,md5(子表的起始相加)不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '距离不能为空', trigger: 'blur' }
          ],
          cost: [
            { required: true, message: '成本不能为空', trigger: 'blur' }
          ],
          estimatedTime: [
            { required: true, message: '预计时间不能为空', trigger: 'blur' }
          ],
          transferCount: [
            { required: true, message: '中转次数不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '从1开始递增不能为空', trigger: 'blur' }
          ],
          isCurrent: [
            { required: true, message: '是否是当前最新版本 0 否 1 是不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dispatch/cacheline/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.startAgencyId = data.cacheLine.startAgencyId
                this.dataForm.endAgencyId = data.cacheLine.endAgencyId
                this.dataForm.verifyKey = data.cacheLine.verifyKey
                this.dataForm.distance = data.cacheLine.distance
                this.dataForm.cost = data.cacheLine.cost
                this.dataForm.estimatedTime = data.cacheLine.estimatedTime
                this.dataForm.transferCount = data.cacheLine.transferCount
                this.dataForm.version = data.cacheLine.version
                this.dataForm.isCurrent = data.cacheLine.isCurrent
                this.dataForm.createDate = data.cacheLine.createDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/dispatch/cacheline/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'startAgencyId': this.dataForm.startAgencyId,
                'endAgencyId': this.dataForm.endAgencyId,
                'verifyKey': this.dataForm.verifyKey,
                'distance': this.dataForm.distance,
                'cost': this.dataForm.cost,
                'estimatedTime': this.dataForm.estimatedTime,
                'transferCount': this.dataForm.transferCount,
                'version': this.dataForm.version,
                'isCurrent': this.dataForm.isCurrent,
                'createDate': this.dataForm.createDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
